<?php


namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

/**
 * 获奖
 */
class WinrecordController extends AdminbaseController
{
	
	
	protected function getGame($k = '')
	{
		$game = array('1' => '智勇三张', '2' => '海盗船长', '3' => '转盘', '4' => '开心牛仔', '5' => '二八贝',);
		if($k === ''){
			return $game;
		}
		
		return $game[$k] ?? '';
	}
	
	// 消费记录（以前的）
	function index()
	{
		$data = $this->request->param();
		$map  = [];
		
		$start_time = $data['start_time'] ?? '';
		$end_time   = $data['end_time'] ?? '';
		
		if($start_time != ""){
			$map[] = ['addtime', '>=', strtotime($start_time)];
		}
		
		if($end_time != ""){
			$map[] = ['addtime', '<=', strtotime($end_time) + 60 * 60 * 24];
		}
		
		$type = $data['type'] ?? '';
		if($type != ''){
			$map[] = ['type', '=', $type];
		}
		
		$action = $data['action'] ?? '';
		if($action != ''){
			$map[] = ['action', '=', $action];
		}
		
		$uid = $data['uid'] ?? '';
		if($uid != ''){
			$lianguid = getLianguser($uid);
			if($lianguid){
				//$map[]=['uid',['=',$uid],['in',$lianguid],'or'];
				array_push($lianguid, $uid);
				$map[] = ['uid', 'in', $lianguid];
			} else{
				$map[] = ['uid', '=', $uid];
			}
		}
		
		$touid = $data['touid'] ?? '';
		if($touid != ''){
			$map[] = ['touid', '=', $touid];
		}
		
		$lists = Db::name("user_coinrecord")
				   ->where($map)
				   ->order("id desc")
				   ->paginate(20);
		
		$lists->each(function($v, $k) {
			$v['userinfo']   = getUserInfo($v['uid']);
			$v['touserinfo'] = getUserInfo($v['touid']);
			
			$action = $v['action'];
			if($action == '1'){
				$giftinfo = Db::name("gift")->field("giftname")->where("id='{$v['giftid']}'")->find();
			} elseif($action == '3'){
				$giftinfo['giftname'] = '第'.$v['giftid'].'天';
			} elseif($action == '4'){
				$info                 = Db::name("vip")->field("name")->where("id='{$v['giftid']}'")->find();
				$giftinfo['giftname'] = $info['name'];
			} elseif($action == '5'){
				$info                 = Db::name("car")->field("name")->where("id='{$v['giftid']}'")->find();
				$giftinfo['giftname'] = $info['name'];
			} elseif($action == '18'){
				$info                 = Db::name("liang")->field("name")->where("id='{$v['giftid']}'")->find();
				$giftinfo['giftname'] = $info['name'];
			} elseif($action == '10'){
				$info                 = Db::name("guard")->field("name")->where("id='{$v['giftid']}'")->find();
				$giftinfo['giftname'] = $info['name'];
			} elseif($action == '19' || $action == '20'){
				$info                 = Db::name("game")->field("action")->where("id='{$v['giftid']}'")->find();
				$giftinfo['giftname'] = $this->getGame($info['action']);
			} else{
				$giftinfo['giftname'] = $this->getAction($action);
				
			}
			$v['giftinfo'] = $giftinfo;
			
			return $v;
		});
		
		$lists->appends($data);
		$page = $lists->render();
		
		$this->assign('lists', $lists);
		$this->assign("page", $page);
		$this->assign('action', $this->getAction());
		$this->assign('type', $this->getTypes());
		
		return $this->fetch();
		
	}
	
	// 送礼获奖记录
	public function gift()
	{
		$data = $this->request->param();
		$map  = [];
		
		$map[]      = ['action', '=', 12];
		$start_time = $data['start_time'] ?? '';
		$end_time   = $data['end_time'] ?? '';
		if($start_time != ""){
			$map[] = ['addtime', '>=', strtotime($start_time)];
		}
		if($end_time != ""){
			$map[] = ['addtime', '<=', strtotime($end_time) + 60 * 60 * 24];
		}
		
		$type = $data['type'] ?? '';
		if($type != ''){
			$map[] = ['type', '=', $type];
		}
		
		$uid = $data['uid'] ?? '';
		if($uid != ''){
			$map[] = ['uid', '=', $uid];
			// 以前的条件
			/*$lianguid = getLianguser($uid);
			if($lianguid){
				//$map[]=['uid',['=',$uid],['in',$lianguid],'or'];
				array_push($lianguid, $uid);
				$map[] = ['uid', 'in', $lianguid];
			} else{
				$map[] = ['uid', '=', $uid];
			}*/
		}
		
		$touid = $data['touid'] ?? '';
		if($touid != ''){
			$map[] = ['touid', '=', $touid];
		}
		
		// 查询统计
		// 下注总数 获奖总数 下注-获奖
		//$lists = Db::name("user_coinrecord")->where($map)->order("id desc")->select()->toArray();
		
		// 查询
		$lists = Db::name("user_coinrecord")->where($map)->order("id desc")->paginate(20);
		// 礼物信息
		$giftIds = $lists->column('giftid');
		$gifts   = Db::name("gift")->where('id', 'in', $giftIds)->order("id desc")->column('*', 'id');
		$lists->each(function($v, $k) use ($gifts) {
			//$giftinfo['giftname'] = '礼物中奖';
			
			$v['giftinfo']   = $gifts[$v['giftid']] ?? [];
			$v['userinfo']   = getUserInfo($v['uid']);
			$v['touserinfo'] = getUserInfo($v['touid']);
			
			return $v;
		});
		
		$lists->appends($data);
		$page = $lists->render();
		$this->assign('lists', $lists);
		$this->assign("page", $page);
		
		return $this->fetch();
		
	}
	
	
	// 智勇三张游戏获奖记录
	public function game()
	{
		$action  = 1;  // 炸金花游戏
		$map     = [];
		$smap    = [];
		$map[]   = ['action', '=', $action];
		$smap[]  = ['game_action', '=', $action];
		$smap[]  = ['action', '=', 21];
		$data    = $this->request->param();
		$uid     = $data['uid'] ?? '';
		$gameid  = $data['gameid'] ?? '';
		$liveuid = $data['liveuid'] ?? '';
		if($gameid != ''){
			$map[]  = ['gameid', '=', $gameid];
			$smap[] = ['giftid', '=', $gameid];
		}
		if($liveuid != ''){
			$games  = Db::name("game")->where('liveuid', $liveuid)->order("id DESC")->column('id');
			$map[]  = ['gameid', 'in', $games];
			$smap[] = ['giftid', 'in', $games];
		}
		$start_time = $data['start_time'] ?? '';
		$end_time   = $data['end_time'] ?? '';
		if($start_time != ""){
			$map[]  = ['addtime', '>=', strtotime($start_time)];
			$smap[] = ['addtime', '>=', strtotime($start_time)];
		}
		if($end_time != ""){
			$map[]  = ['addtime', '<', strtotime($end_time) + 60 * 60 * 24];
			$smap[] = ['addtime', '<', strtotime($end_time) + 60 * 60 * 24];
		}
		if($uid != ''){
			$map[]  = ['uid', '=', $uid];
			$smap[] = ['uid', '=', $uid];
			// 以前的条件
			/*$lianguid = getLianguser($uid);
			if($lianguid){
				array_push($lianguid, $uid);
				$map[] = ['uid', 'in', $lianguid];
			} else{
				$map[] = ['uid', '=', $uid];
			}*/
		}
		
		// 倍数
		//$configpri       = getConfigPri();
		//$this->game_pump = $configpri['game_pump'];
		
		// 查询
		$lists = Db::name("gamerecord")->where($map)->order("id DESC")->paginate(20);
		// 获取游戏信息  与 主播
		// 获取赢的位置 result
		$gameIds = array_unique($lists->column('gameid'));
		$games   = Db::name("game")->where('id', 'in', $gameIds)->order("id DESC")->column('*', 'id');
		// 获奖
		$scores    = Db::name("user_scorerecord")->where('giftid', 'in', $gameIds)->where('action', 21)->where('game_action', $action)->order("id DESC")->select()->toArray();
		$scoreList = [];
		foreach($scores as $_score){
			$_gameId = $_score['giftid'];
			$_uId    = $_score['uid'];
			// 组合
			$scoreList[$_gameId][$_uId] = $_score;
		}
		
		// 查询统计
		// 下注总数 获奖总数 下注-获奖
		$bets   = Db::name("gamerecord")->where($map)->field("sum(coin_1) coin_1s, sum(coin_2) coin_2s, sum(coin_3) coin_3s")->select()->toArray();  // ->where('status', 1)
		$scores = Db::name("user_scorerecord")->where($smap)->field("sum(totalcoin) totalcoins")->select()->toArray();
		$tatal  = 0;
		$coins  = $scores[0]['totalcoins'] ?? 0;
		foreach($bets as $_bet){
			$tatal += $_bet['coin_1s'];
			$tatal += $_bet['coin_2s'];
			$tatal += $_bet['coin_3s'];
		}
		
		$lists->each(function($v, $k) use ($games, $scoreList) {
			$_gameId = $v['gameid'];
			$result  = $games[$_gameId]['result'] ?? 0;
			if(!empty($games[$_gameId])){
				$games[$_gameId]['userinfo'] = getUserInfo($games[$_gameId]['liveuid']);
			}
			
			/*$total  = 0;
			$total2 = 0;
			if(strstr($result, ',')){
				$result_a = explode(',', $result);
				foreach($result_a as $k1 => $v1){
					$total2 += $v['coin_'.($k1 + 1)];
					if($v1 == 3){
						$total += $v['coin_'.($k1 + 1)];
					}
				}
			} else{
				$total = $v['coin_'.$result];
			}
			$win = $total + floor($total * (2 - 1) * (100 - $this->game_pump) * 0.01) - $total2;*/
			$winSore = $scoreList[$_gameId][$v['uid']]['totalcoin'] ?? 0;
			if($v['coin_'.$result] > 0){
				$v['win'] = $winSore;
			}else{
				$v['win'] = 0;
			}
			
			$v['game']     = $games[$_gameId] ?? [];
			$v['userinfo'] = getUserInfo($v['uid']);
			
			return $v;
		});
		
		$lists->appends($data);
		$page = $lists->render();
		
		// 获取 platform_total_amount
		$platform_total_amount = Db::name("site_config")->where('name', 'platform_total_amount')->value('config');
		
		$this->assign('configPri', getConfigPri());
		$this->assign('bet_total', $tatal);
		$this->assign('bet_coins', $coins);
		$this->assign('lists', $lists);
		$this->assign("page", $page);
		$this->assign("action", $this->getGame());
		$this->assign("gameid", $gameid);
		$this->assign("platform_total_amount", $platform_total_amount);
		
		return $this->fetch();
	}
	
	// 更改设置 config
	public function savePlatformAmount()
	{
		$data  = $this->request->param();
		$value = $data['platform_total_amount'] ?? 0;
		$value = intval($value);
		
		$res = Db::name("site_config")->where('name', 'platform_total_amount')->update(['config' => $value]);
		if($res === false){
			return $this->error('操作失败！');
		} else{
			return $this->success("操作成功！");
		}
	}
	
	// 删除
	public function del()
	{
		$id = $this->request->param('id', 0, 'intval');
		$rs = DB::name('user_scorerecord')->where("id={$id}")->delete();
		if(!$rs){
			$this->error("删除失败！");
		}
		
		$this->success("删除成功！");
	}
}
